
import random
import numpy as np

import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
# from proplot import rc
plt.rcParams['axes.unicode_minus'] = False  # -
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
# rc["font.family"] = "Times New Roman"  # 统一设置字体
# rc["font.family"] = "Arial"  # 统一设置字体
# rc["axes.labelweight"] = "light" # 统一设置轴刻度标签的字体粗细
# rc["tick.labelweight"] = "bold"  # 统一设置xy轴名称的字体粗细

xpixels=800
ypixels=800
dpi=60

xinch = xpixels / dpi
yinch = ypixels / dpi
fig, axes = plt.subplots(3, 1, figsize=(xinch, yinch))

datas = [
{
    '本文方法': [96.64, 97.36, 97.00],
    'PCA': [97.53, 67.02, 79.45],
    'IM': [88.05, 95.14, 91.46],
    'LogCluster': [87.40, 73.98, 80.13],
    'DeepLog': [95.60, 92.79, 94.17],
    'NeuralLog': [96.13, 96.88, 96.50],
},
{
    '本文方法': [97.55, 96.86, 97.20],
    'PCA': [50.32, 60.84, 55.08],
    'IM': [83.43, 95.04, 88.86],
    'LogCluster': [42.24, 87.08, 56.89],
    'DeepLog': [90.44, 95.86, 93.07],
    'NeuralLog': [96.72, 96.18, 96.44],
},
{
    '本文方法': [95.78, 97.48, 96.62],
    'PCA': [25.37, 14.79, 18.69],
    'IM': [88.85, 14.79, 25.36],
    'LogCluster': [56.88, 19.08, 28.57],
    'DeepLog': [93.07, 97.36, 95.17],
    'NeuralLog': [94.08, 97.56, 95.79],
}
]
titles = ["HDFS", "BGL", "OpenStack"]

fontsize = 25
labels = ['Precision', 'Recall', 'F1']
color = ['red', 'orange', 'green', 'blue', 'cyan', 'purple', 'pink', 'coral', 'gold', 'lime', 'navy', 'teal', 'indigo']













for cnt, data in enumerate(datas):
    models = list(data.keys())
    performance = list(data.values())

    for i in range(len(models)):
        zoom = 0.5 if '方法' in models[i] else 1
        value = np.array(performance[i])
        dif = random.random()*zoom
        axes[cnt].plot(range(len(value)), value, color=color[i], linewidth=2, label=models[i], marker='x')
        axes[cnt].fill_between(range(len(value)), value-dif, value+dif, color=color[i], alpha=0.15)

    # 添加标题和标签
    axes[cnt].set_title(titles[cnt], fontsize=fontsize)
    axes[cnt].set_ylabel('性能', fontsize=fontsize)
    # 拉大x轴
    axes[cnt].set_xlim(-0.2, 2.2)
    axes[cnt].tick_params(labelsize=fontsize*0.9)
    if cnt == 0:
        # 显示图例
        axes[cnt].legend(fontsize=fontsize*0.8, loc='lower left', ncol=2, framealpha=0.2)
    if cnt == 2:
        # 修改x刻度
        axes[cnt].set_xticks(range(len(value)), labels)
    else:
        # 修改x刻度
        axes[cnt].set_xticks([])

plt.tight_layout()

# 显示图形
# plt.show()
plt.savefig(f'performance.pdf')
plt.savefig(f'performance.png')
plt.close()